package com.tianpengtech.admin.fairauth;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.tianpengtech.admin.AbstractAdminController;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.InitInterceptor;
import com.tianpengtech.admin.authenticate.AuthenticateAdminServiceImpl;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.fairauth.FairAuth;
import com.tianpengtech.common.model.fairwithdraw.FairWithdraw;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.util.JsonUtil;

import static com.tianpengtech.common.model.opertion.Opertion.OPT_FAIR_AUTH_LOG;
import static com.tianpengtech.common.model.opertion.Opertion.OPT_FAIR_GET_CASH;

/**
* 路由配置地址：add("/admin/fairauth", FairAuthAdminController.class, "/admin/fairauth");
* 数据库映射地址：arp.addMapping(FairAuth.dao.tableName(),FairAuth.class);
*/
@Before(InitInterceptor.class)
public class FairAuthAdminController extends AbstractAdminController{
	private IAdminOpt opt = new FairAuthAdminServiceImpl();
	@Override
	protected void config() {
		this.clazz=FairAuth.class;
		this.service=new FairAuthAdminServiceImpl();
	}

	@Override
	public void beforeAdd() {

	}

	@Override
	public void beforeAdd(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model< ? > model) {

	}

	/**
	 * 审核通过
	 */
	public void  setFairPrice(){
		int id = getParaToInt(FairAuth.ID,0);
		FairAuth fairAuth =FairAuth.dao.findById(id);
		Fair fair;
		if(fairAuth!=null){
			fair = Fair.dao.findById(fairAuth.getLong(FairAuth.WH_ID));
			setAttr("fair", fair);
		}
		setAttr("fairAuth", fairAuth);
		renderTemplate("set-fair-price.html");
	}
	
	/**
	 * 审核通过
	 */
	
	@Before(Tx.class)
	public void  pass(){
		int id = getParaToInt(FairAuth.ID,0);
		int price = getParaToInt("price",0);
		try {
			opt.changeStatus(getModel(FairAuth.class), id, FairAuth.STATUS_SUCCESS, Kv.create().set("price", price));
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}
	
	/**
	 * 拒绝理由填写页面
	 */
	public void  toRefuse(){
		int id = getParaToInt(FairAuth.ID,0);
		String sql = "select * from fair_admin_opt_log where opt_type=? and obj_id=?";
		Opertion opertion = Opertion.dao.findFirst(sql, OPT_FAIR_AUTH_LOG, id);
		if (opertion != null) setAttr(Opertion.CONTENT, opertion.getStr(Opertion.CONTENT));
		setAttr("fairAuth",this.service.queryById(getModel(FairAuth.class), id));
		renderTemplate("fairauth-reason.html");
	}
	
	/**
	 * 审核不通过
	 */
	@Before(Tx.class)
	public void  unpass(){
		FairAuth fairauth = getModel(FairAuth.class);
		String content = getPara(Opertion.CONTENT);
		int price = getParaToInt("price",0);
		Kv kv = Kv.by(Opertion.CONTENT, content);
		try {
			opt.changeStatus(fairauth, fairauth.getInt(FairAuth.ID), FairAuth.STATUS_FAILURE, kv.set("price", price));
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}
	
	@Override
	public void parsePageRecord(Object results) {
		FairAuth r = (FairAuth)results;
		Fair fair= Fair.dao.findById(r.getInt(FairAuth.WH_ID));
		r.set(FairAuth.TYPE, r.type());
		r.set(FairAuth.STATUS, r.status());
		r.set(FairAuth.WH_ID, fair.getStr(Fair.NICK_NAME));
	}
}

	